<template>
  <div class="main-layout">
    <!-- 统一的头部导航 -->
    <el-header class="header">
      <div class="header-content">
        <div class="logo" @click="router.push('/home')">TimeShare</div>
        <el-menu
          mode="horizontal"
          :default-active="activeMenu"
          @select="handleMenuSelect"
          class="nav-menu"
        >
          <el-menu-item index="/home">首页</el-menu-item>
          <el-menu-item index="/skill">技能广场</el-menu-item>
          <el-menu-item index="/appointment">我的预约</el-menu-item>
          <el-menu-item index="/chat">消息</el-menu-item>
          <el-menu-item index="/notice">公告</el-menu-item>
          <el-menu-item v-if="userStore.userInfo?.role === 1" index="/admin">管理后台</el-menu-item>
        </el-menu>
        <div class="user-info" @click="router.push('/profile')">
          <el-avatar :src="userStore.userInfo?.avatar || ''" :size="36">
            {{ userStore.userInfo?.nickname?.[0] || 'U' }}
          </el-avatar>
          <span class="nickname">{{ userStore.userInfo?.nickname }}</span>
        </div>
      </div>
    </el-header>

    <!-- 页面内容区域 -->
    <div class="main-content">
      <router-view />
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const activeMenu = computed(() => {
  // 处理带参数的路由，如 /skill/detail/1 应该高亮 /skill
  const path = route.path
  if (path.startsWith('/skill')) return '/skill'
  if (path.startsWith('/appointment')) return '/appointment'
  return path
})

const handleMenuSelect = (index) => {
  router.push(index)
}
</script>

<style scoped>
.main-layout {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(to bottom, #f8f9ff 0%, #f5f7fa 100%);
}

.header {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  box-shadow: 0 2px 20px rgba(102, 126, 234, 0.1);
  padding: 0;
  position: sticky;
  top: 0;
  z-index: 100;
  animation: slideInLeft 0.5s ease-out;
}

.header-content {
  max-width: 1400px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  padding: 0 24px;
}

.logo {
  font-size: 28px;
  font-weight: 800;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  cursor: pointer;
  transition: all 0.3s;
  letter-spacing: -0.5px;
}

.logo:hover {
  transform: scale(1.05);
}

.nav-menu {
  flex: 1;
  margin: 0 40px;
  border: none;
  background: transparent;
}

.nav-menu :deep(.el-menu-item) {
  font-weight: 500;
  transition: all 0.3s;
  border-radius: 8px;
  margin: 0 4px;
}

.nav-menu :deep(.el-menu-item:hover) {
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
}

.nav-menu :deep(.el-menu-item.is-active) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white !important;
  font-weight: 600;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  padding: 8px 16px;
  border-radius: 24px;
  transition: all 0.3s;
}

.user-info:hover {
  background: rgba(102, 126, 234, 0.08);
  transform: translateY(-2px);
}

.nickname {
  font-size: 14px;
  font-weight: 500;
  color: #333;
}

.main-content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-content {
    padding: 0 16px;
  }

  .logo {
    font-size: 22px;
  }

  .nav-menu {
    margin: 0 20px;
  }

  .nickname {
    display: none;
  }
}
</style>

